package cn.ncu.wechat.controller;

import cn.ncu.wechat.model.MemberLevel;
import cn.ncu.wechat.service.MemberLevelService;
import cn.ncu.wechat.service.UserService;
import cn.ncu.wechat.util.Result;
import cn.ncu.wechat.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/admin")
public class MemberController {
    @Autowired
    private UserService userService;
    @Autowired
    private MemberLevelService memberLevelService;
    @GetMapping("/members")
    public Result<?> searchMemberList(@RequestParam String keyword,@RequestParam Integer page,@RequestParam Integer pageSize){
        if(page<=0||pageSize<=0||pageSize==null||page==null){
            return Result.error(400,"参数错误");
        }
        else return userService.searchUserListByKeyWord(keyword,page,pageSize);
    }

    @GetMapping("/marketing/member-levels")
    public Result<?> getMemberList(){
        return memberLevelService.getMemberList();
    }

    @PostMapping ("/marketing/member-levels")
    public Result<?> UpdateMemberList(@RequestBody MemberLevel memberLevel){
        return memberLevelService.updateMemberList(memberLevel);
    }

    @GetMapping("/member-registrations")
    public Result<?> getRegisterRecord(@RequestParam Integer page, @RequestParam Integer pageSize,@RequestParam String startDate,@RequestParam String endDate){
        if (page<=0||page==null
                ||pageSize<=0||pageSize==null
                || !StringUtil.IsNotNull(startDate)
                ||!StringUtil.IsNotNull(endDate)) return Result.error(400,"参数错误");
        return userService.getRegisterRecord(page,pageSize,startDate,endDate);
    }

    @GetMapping("/members/{id}/detail")
    public Result<?> getMemberDetail(@PathVariable String id){
        Integer uid = null;
        uid = Integer.parseInt(id);
        if (uid==null){
            return Result.fail(400,"参数错误");
        }
        return userService.getMemberDetail(uid);
    }

    @GetMapping("/members/{id}/orders")
    public Result<?> getMemberOrders(@PathVariable Long id,@RequestParam Integer page,@RequestParam Integer pageSize,@RequestParam Integer status){
        if (page<=0||page==null||id==null||id==0
                ||pageSize<=0||pageSize==null){
            return Result.fail(400,"参数错误");
        }
        return userService.getMemberOrders(id,page,pageSize,status);
    }

    @GetMapping("/members/statistics")
    public Result<?> CountMember(){
        return userService.CountMember();
    }
}
